﻿using System;
using System.Collections.Generic;
using SDT.CrawlSystem.Servers.Filters;
using SDT.Libraries.Utilities;
using System.Text.RegularExpressions;
using System.Web;

namespace URLChannelTester
{
    /// <summary>
    /// Written by TungNX
    /// </summary>
    public static class UrlParser
    {

        public static List<ImgDes> FindUrls(string html, IUrlFilter filter, string baseURL)
        {
            List<ImgDes> list = new List<ImgDes>();
            MatchCollection m1 = Regex.Matches(html, @"(<a.*?>.*?</a>)", RegexOptions.Singleline);
            foreach (Match m in m1)
            {
                string value = m.Groups[1].Value;
                ImgDes i = new ImgDes();
                //i.UrlImage =
                if (value.Contains("href=\""))
                {
                    Match m2 = Regex.Match(value, @"href=\""(.*?)\""", RegexOptions.Singleline);
                    if (m2.Success)
                    {
                        string url = m2.Groups[1].Value;
                        url = RemoveCharacter(url);
                        if (filter.AcceptUrl(url) && !list.Exists(itm => itm.Href == url))
                        {
                            ImgDes imgObj = new ImgDes();
                            //imgObj.Href = u;
                            //get Link Image follow if have
                            //var endImg = html.IndexOf(@"</a>", end, html.Length - end );
                            //if (endImg > end)
                            //{
                            //    var img = html.Substring(end, endImg - end);
                            //    string urlImge = Utility.GetUrlSrc(baseURL, img);
                            //    imgObj.UrlImage = urlImge;
                            //}

                            i.Href = url;
                            list.Add(i);
                        }
                    }
                }
                // http://autonet.com.vn
                else if (value.Contains("href=\\\""))
                {
                    Match m2 = Regex.Match(value, @"href=\\""(.*?)\\""", RegexOptions.Singleline);

                    if (m2.Success)
                    {
                        string url = m2.Groups[1].Value;
                        url = RemoveCharacter(url);
                        if (filter.AcceptUrl(url) && !list.Exists(itm => itm.Href == url))
                        {
                            //get Link Image follow if have
                            //var endImg = html.IndexOf(@"</a>", end, html.Length - end );
                            //if (endImg > end)
                            //{
                            //    var img = html.Substring(end, endImg - end);
                            //    string urlImge = Utility.GetUrlSrc(baseURL, img);
                            //    imgObj.UrlImage = urlImge;
                            //}
                            i.Href = url;
                            list.Add(i);
                        }
                    }
                }
                else
                {
                    Match m2 = Regex.Match(value, @"(?<=href=')(.*?)(?=')", RegexOptions.Singleline);

                    if (m2.Success)
                    {
                        string url = m2.Groups[1].Value;
                        url = RemoveCharacter(url);
                        if (filter.AcceptUrl(url) && !list.Exists(itm => itm.Href == url))
                        {
                            //get Link Image follow if have
                            //var endImg = html.IndexOf(@"</a>", end, html.Length - end );
                            //if (endImg > end)
                            //{
                            //    var img = html.Substring(end, endImg - end);
                            //    string urlImge = Utility.GetUrlSrc(baseURL, img);
                            //    imgObj.UrlImage = urlImge;
                            //}
                            i.Href = url;
                            list.Add(i);
                        }
                    }
                }
            }
            return list;
        }

        /// <summary>
        /// Modified Date :   
        ///remove ../    
        /// Add function RemoveCharacter(u);
        /// </summary>
        /// <param name="html"></param>
        /// <param name="filter"></param>
        /// <param name="baseURL"></param>
        /// <returns></returns>
        public static IList<ImgDes> ParserUrls(string html, IUrlFilter filter, string baseURL)
        {
            if (html == null || html.Length == 0)
                return new List<ImgDes>();

            var l = new List<ImgDes>();
            int i = 0;
            while (i < html.Length - "href=\"".Length)
            {
                #region Check "href" sign
                //luu trang thai if href=\" thi _isQuouteMark=false
                //nguoc lai href=\' thi  _isQuouteMark = true
                bool _isQuouteMark = false;

                if (html[i + 0] == 'h' &&html[i + 1] == 'r' &&html[i + 2] == 'e' &&html[i + 3] == 'f')
                #endregion
                {
                    #region Normal case
                    if (html[i + 4] == '=' && (html[i + 5] == '"' || html[i + 5] == '\''))
                    {
                        //Modified Date : 25/10/2010
                        //truong hop the href=" va truong hop href='
                       
                        if (html[i + 5] == '"')
                            i += "href=\"".Length;
                        else
                        {
                            if (html[i + 5] == '\'') i += "href=\'".Length;
                            _isQuouteMark = true;

                        }

                        var end = html.IndexOf(_isQuouteMark ? "\'" : "\"", i, Math.Min(255, html.Length - i > 0 ? html.Length - i : 255));
                        if (end > i)
                        {
                            var u = html.Substring(i, end - i);
                            //remove ../    
                            u = RemoveCharacter(u);

                            if (filter.AcceptUrl(u) && !l.Exists(itm => itm.Href == u))
                            {
                                ImgDes imgObj = new ImgDes();
                                imgObj.Href = u;
                                //get Link Image follow if have
                                var endImg = html.IndexOf(@"</a>", end, html.Length - end);
                                if (endImg > end)
                                {
                                    var img = html.Substring(end, endImg - end);
                                    string urlImge = Utility.GetUrlSrc(baseURL, img);
                                    imgObj.UrlImage = urlImge;
                                }
                                l.Add(imgObj);
                                //end get Link Image

                            }
                            i = end + 1;
                        }

                    }
                    #endregion
                    #region Special case
                    else
                    {
                        i += "href=\"".Length;

                        var sta = html.IndexOf(_isQuouteMark ? "\'" : "\"", i, Math.Min(255, html.Length - i > 0 ? html.Length - i : 255));
                        if (sta > 0)
                        {
                            sta++;
                            var end = html.IndexOf(_isQuouteMark ? "\'" : "\"", sta, Math.Min(255, html.Length - i > 0 ? html.Length - i : 255));
                            if (end > sta)
                            {
                                var u = html.Substring(sta, end - sta);
                                //remove ../    
                                u = RemoveCharacter(u);

                                if (filter.AcceptUrl(u) && !l.Exists(itm => itm.Href == u))
                                {
                                   

                                    ImgDes imgObj = new ImgDes();
                                    imgObj.Href = u;
                                    //get Link Image follow if have
                                    var endImg = html.IndexOf("</a>", end, Math.Min(255, html.Length - i > 0 ? html.Length - i : 255));
                                    if (endImg > end)
                                    {
                                        var img = html.Substring(end, endImg - end);
                                        string urlImge = Utility.GetUrlSrc(baseURL, img);
                                        imgObj.UrlImage = urlImge;
                                    }
                                    l.Add(imgObj);
                                    //end get Link Image

                                }

                            }
                            i = end + 1;
                        }
                    }

                    #endregion
                }

                else
                    i++;
            }
            return l;
        }
        /// <summary>
        ///  //remove ../
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string RemoveCharacter(string url)
        {
             string sEx = "../";
            int index = url.LastIndexOf(sEx);
            if (index != -1) url = url.Substring(index + sEx.Length - 1);

            return ConvertWhitespacesToSingleSpaces(url);    
        }
        public static string ConvertWhitespacesToSingleSpaces(string value)
        {
            return Regex.Replace(value, @"\s", "");
        }
    }
}